import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//引入axios
import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios
import routes from './routes'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
//import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //NProgress.start();
  let token = sessionStorage.getItem('token')
  if (to.path!=='/login'&&to.path!=='/shopRegister'){
    if (!token){
      console.log('未登录')
      next({path:'/login'})
    }
  }
    next()
  /*if (to.path == '/login' || to.path=='/shopRegister') {
    //sessionStorage.removeItem('user');
    next()
  }else {
    let token = sessionStorage.getItem('token')
    if (!token){
      next({path:'/login'})
    }else {
      next()
    }
  }*/
})
//前置拦截器
axios.interceptors.request.use(res=>{
  let token = sessionStorage.getItem('token')
  if (token){
    res.headers['token']=token
  }
  return res
})
//router.afterEach(transition => {
//NProgress.done();
//});
//后置拦截器
axios.interceptors.response.use(res=>{
  if (res.data.flag&&res.data.msg=='userLogin'){
    //登录成功,存放token和user信息
    let token = res.data.data.token;
    let user = JSON.stringify(res.data.data.user);
    sessionStorage.setItem('token',token)
    sessionStorage.setItem('user',user)
    return res
  }
  if (res.data.flag&&res.data.msg=='noLogin'){
    //清除token和user信息
    sessionStorage.removeItem('token')
    sessionStorage.removeItem('user')
    return res
  }
  return res
})
new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

